package com.zsj.service_user.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.zsj.common_util.R;
import com.zsj.service_user.entity.AppGoods;
import com.zsj.service_user.entity.AppUser;
import com.zsj.service_user.entity.pojo.AvatarName;
import com.zsj.service_user.entity.pojo.LoginInfo;
import com.zsj.service_user.mapper.AppUserMapper;
import com.zsj.service_user.service.AppUserService;
import com.zsj.service_user.service.impl.AppUserServiceImpl;
import com.zsj.service_user.util.TokenMd5;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zsj
 * @since 2022-04-27
 */
@RestController
@RequestMapping("/serviceUser/user")
@CrossOrigin
@Slf4j
public class AppUserController {


    @Autowired
    private AppUserService appUserService;


    /**
     * 用户登录
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginInfo info) {
        if (appUserService.haveSomeOne(info.getUsername(), info.getPassword())) {
            return R.ok().message("登录成功").data("token", TokenMd5.getMD5(info.getUsername()));
        } else {
            return R.error().message("登录失败！用户名不存在或者密码错误");
        }

    }

    /**
     * 注册用户
     *
     * @param user
     * @return
     */
    @PostMapping("/register")
    public R registerUser(@RequestBody AppUser user) {
        Boolean save = appUserService.registerUser(user);
        if (save) {
            return R.ok().message("注册成功!");
        } else return R.error().message("注册失败,该账号已经被使用了");
    }


    /**
     * 获取当前用户全部信息
     * 根据账号名来读取,并且要求带token参数
     */
    @GetMapping("/info/{name}")
    public R getUserInfoByName(@RequestParam("token") String token, @PathVariable("name") String name) {
        if (TokenMd5.getMD5(name).equals(token)) {
            QueryWrapper<AppUser> queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_name", name);
            AppUser user = appUserService.getOne(queryWrapper);
            return R.ok().message("获取成功").data("info", user);
        } else {
            return R.error().message("身份认证失败!");
        }
    }


    /**
     * 用户头像更新
     */
    @PostMapping("/updateAvatar")
    public R updateAvatar(@RequestBody AvatarName info) {
        if (appUserService.updateAvatarByName(info)) {
            log.info("修改成功,用户名为" + info.getName());
            return R.ok().message("修改成功");
        } else {
            log.error("修改失败" + info.getName());
            return R.error().message("修改失败");
        }
    }

    /**
     * 根据用户名获取到当前用户的头像
     */
    @GetMapping("/getUserAvatarByName")
    public R getUserAvatarByName(@RequestParam("name") String name) {
        String url = appUserService.getUserAvatarByName(name);
        if (url.isEmpty() || url.equals("")) {
            log.error("查询错误在getUserAvatarByName方法中");
            return R.error().message("查询失败");
        } else {
            log.info("查询成功在getUserAvatarByName方法中");
            return R.ok().message("查询成功").data("url", url);
        }
    }


    /**
     * 查询用户余额
     */
    @PostMapping("getBalance")
    public R getBalance(@RequestParam("token") String token, @RequestParam("name") String name) {
        if (TokenMd5.getMD5(name).equals(token)) {
            //拿到用户余额并且返回
            return R.ok().data("balance", appUserService.getBalance(name));
        } else return R.error().message("身份验证失败");

    }


    /**
     * 充值
     */
    @PostMapping("addBalance")
    public R UpdateBalance(@RequestParam("token") String token,
                           @RequestParam("name") String name,
                           @RequestParam("price") Integer price) {
        if (TokenMd5.getMD5(name).equals(token)) {
            //拿到用户余额并且返回
            if (appUserService.addBalance(name, price)) {
                return R.ok().message("充值成功");
            } else {
                return R.error().message("充值失败");
            }
        } else return R.error().message("身份验证失败");
    }


    /**
     * 修改个性名称
     */
    @PostMapping("/upNickName")
    public R updateNickName(@RequestParam("name") String name, @RequestParam("newName") String newName) {
        if (appUserService.upNickName(name, newName)) {
            return R.ok().message("修改成功");
        } else return R.error();
    }

    /**
     * 修改性别
     */
    @PostMapping("/upSex")
    public R upSex(@RequestParam("name") String name, @RequestParam("newSex") String newSex) {
        if (appUserService.upSex(name, newSex)) {
            return R.ok().message("修改成功");
        } else return R.error();
    }

    /**
     * 修改生日
     */
    @PostMapping("/upBirthday")
    public R upBirthday(@RequestParam("name") String name, @RequestParam("newBirthday") String newBirthday) {
        if (appUserService.upBirthday(name, newBirthday)) {
            return R.ok().message("修改成功");
        } else return R.error();
    }

    /**
     * 修改密码
     */
    @PostMapping("/upPassword")
    public R upPassword(@RequestParam("name") String name, @RequestParam("oldPass") String oldPass, @RequestParam("newPass") String newPass) {
        if (appUserService.upPassword(name,oldPass, newPass)) {
            return R.ok().message("修改成功");
        } else return R.error().message("修改密码失败,旧密码错误或其它异常");
    }

}

